<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Cron
 *
 * @category	Admin
 */
class CronAction extends AdminAction
{


	protected $Cron;


	//初始化
	public function _initialize()
	{
		parent::_initialize();
		$this->Cron = D('Cron');
	}


	//任务列表
	public function cronList()
	{
	    $cronList = $this->Cron->select();
	    $this->assign('cronList', $cronList);
        $this->display();
	}


	//添加计划任务
	public function addCron()
	{
	    $this->editCron();
	}


	//编辑计划任务
	public function editCron()
	{
	    $cronID = intval($_REQUEST['cron_id']);
	    $cronInfo = $this->Cron->find($cronID);

	    if ($this->isPost())
	    {
            $Cron = A('Cron');
            if (!method_exists($Cron, $_POST['method_name']))
            {
            	$this->error(L('cron.cron_method_not_exist'));
            }
            if (!empty($_POST['minute']))
            {
            	$values = explode(',', $_POST['minute']);
            	foreach ($values as $value)
            	{
            		$value = intval(trim($value));
            		if ($value < 0 || $value > 59)
            		{
            			$this->error(L('cron.cron_minute_invalid'));
            		}
            		else
            		{
            		    $minutes[] = $value;
            		}
            	}
            	asort($minutes);
            }
            $map = array(
                'cron_id' => array('neq', $cronID),
                'method_name' => trim($_POST['method']),
            );
            if ($this->Cron->where($map)->count())
            {
            	$this->error(L('cron.cron_exist'));
            }
            $data = array(
                'method_name' => trim($_POST['method_name']),
                'status'      => intval($_POST['status']),
                'weekday'     => intval($_POST['weekday']),
                'day'         => intval($_POST['day']),
                'hour'        => intval($_POST['hour']),
                'minute'      => implode(',', $minutes),
                'description' => $_POST['description'],
            );
            if (empty($cronInfo))
            {
            	$this->Cron->add($data);
            	$message = L('cron.cron_added_succeed');
            }
            else
            {
                $map = array('cron_id' => $cronID);
                $this->Cron->where($map)->save($data);
                $message = L('cron.cron_edited_succeed');
            }

            $this->Cron->cacheCronJobs();

            $this->assign('jumpUrl', __URL__ .'/cronList');
            $this->success($message);
	    }
	    else
	    {
	        $weekdayOptions = array(
	          '-1' => '*',
	           '1'  => L('monday'),
	           '2'  => L('tuesday'),
	           '3'  => L('wendnesday'),
	           '4'  => L('thursday'),
	           '5'  => L('friday'),
	           '6'  => L('saturday'),
	           '7'  => L('sunday'),
	        );
	        $this->assign('weekdayOptions', $weekdayOptions);
	        $this->assign('weekdaySelected', $cronInfo['weekday']);

	        for ($i=-1; $i<=31; $i++)
	        {
	            $dayOptions[$i] = (-1 == $i) ? '*' : $i;
	        }
	        unset($dayOptions[0]);
	        $this->assign('dayOptions', $dayOptions);
	        $this->assign('daySelected', $cronInfo['day']);

	        for ($i=-1; $i<=23; $i++)
	        {
	        	$hourOptions[$i] = (-1 == $i) ? '*' : $i;
	        }
	        $this->assign('hourOptions', $hourOptions);
	        $this->assign('hourSelected', $cronInfo['hour']);

	        $statusRadios = array(
	           '1' => L('yes'),
	           '0' => L('no'),
	        );
	        $this->assign('statusRadios', $statusRadios);
	        $this->assign('statusChecked', $cronInfo['status']);

	        $this->assign('rs', $cronInfo);
	        $this->display('editCron');
	    }
	}


	//计划任务批量管理
	public function manageCron()
	{
	     if ($this->isPost())
	     {
	         if (!empty($_POST['crons']))
	         {
	         	foreach ($_POST['crons'] as $key => $value)
	         	{
	               $map = array('cron_id' => $key);
	               $status = isset($_POST['status'][$key]) ? intval($_POST['status'][$key]) : 0;
	               $this->Cron->where($map)->setField('status', $status);
	         	}
	         }
	         if(!empty($_POST['mods']))
	         {
                 $map = array(
             	    'cron_id' => array('in', $_POST['mods']),
             	    'type' => array('neq', 'system'),
             	 );
             	 $this->Cron->where($map)->delete();
    	     }

    	     $this->Cron->cacheCronJobs();

    	     $this->success(L('cron.cron_updated_succeed'));
	     }
	}


	//执行计划任务
	public function runCron()
	{
	    $cronID = intval($_GET['cron_id']);
	    $Cron = A('Cron');
	    $Cron->runCron($cronID);

	    $this->success(L('cron.cron_finished'));
	}

}
// End  Class

/* End of file CronAction.class.php */
/* Location: ./App/Lib/Action/CronAction.class.php */